package com.robot.controller;

import com.robot.dao.InterfaceMapper;
import com.robot.dao.TestCaseMapper;
import com.robot.model.Protocol;
import com.robot.model.TestCase;
import com.robot.service.AssertExpressionService;
import com.robot.service.TestCaseService;
import com.robot.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/10/20
 * Time: 13:39
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/testCase")
public class TestCaseController {

    @Autowired
    AssertExpressionService assertExpressionService;

    @Autowired
    InterfaceMapper interfaceMapper;
    @Autowired
    TestCaseService testCaseService;
    @Autowired
    TestCaseMapper testCaseMapper;

    @RequestMapping("/page")
    public ModelAndView testPage(Integer id, String interfaceType) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("interfaceId", id);
        modelAndView.addObject("interfaceType", interfaceType);
        modelAndView.setViewName("/testcase/caseList");
        return modelAndView;
    }

    @RequestMapping("/addTestCasePage")
    public ModelAndView addPage(Integer interfaceId) {
        ModelAndView modelAndView = new ModelAndView();
        Protocol protocol = interfaceMapper.getProtocolById(interfaceId);
        String interfaceType = protocol.getInterfaceType();
        if (interfaceType.equalsIgnoreCase("dubbo")) {
            modelAndView.setViewName("/testcase/dubboTestCaseForm");
            modelAndView.addObject("interfaceId", interfaceId);
            modelAndView.addObject("paramModel", protocol.getParam());
        } else if (interfaceType.equalsIgnoreCase("http")) {
            modelAndView.setViewName("");
        }
        return modelAndView;
    }
    @RequestMapping("/modifyTestCasePage")
    public ModelAndView modifyPage(Integer id,String interfaceType){
        ModelAndView modelAndView=new ModelAndView();
        TestCase testCase =testCaseMapper.getTestCaseById(id);
        if (interfaceType.equalsIgnoreCase("dubbo")) {
            modelAndView.setViewName("/testcase/dubboTestCaseForm");
            //当我们不指定key的时候，默认会按照当前类的类型作为关键字的key
            modelAndView.addObject("testCase",testCase);
        } else if (interfaceType.equalsIgnoreCase("http")) {
            modelAndView.setViewName("");
        }
        return modelAndView;
    }

    @RequestMapping("/saveTestCase")
    public @ResponseBody
    ResponseMessage saveTestCase(TestCase testCase) {
        Integer id =testCase.getId();
        if(null == id){
            return testCaseService.addTestCase(testCase);
        }else {
            return testCaseService.modifyTestCase(testCase);
        }
    }
    @RequestMapping("/deleteTestCase")
    public @ResponseBody
    ResponseMessage delete(Integer[] caseIds){
        return testCaseService.delete(caseIds);
    }
    @RequestMapping("/testCaseList")
    public @ResponseBody ResponseMessage list(TestCase testCase){
        return testCaseService.testCaseList(testCase);
    }
    @RequestMapping("/expressionCheck")
    public @ResponseBody ResponseMessage check(Integer assertType,String expectResult,String expression ){
            return assertExpressionService.check(assertType,expectResult,expression);
    }
}
